
/*
 * $HeadURL: http://catwork.googlecode.com/svn/trunk/root/static/js/catwork/apidoc.js $
 * $Id: apidoc.js 2 2011-03-20 16:14:09Z hg.winkler.77 $
*/

$(function () {
    // that the enter works
    $('#search').keyup(function(e) {
        if(e.keyCode == 13) {
            search();
        }
    });
    // initial load of apidoc list
    get_apidoc_list();
    // initial load of catwork pod
    // FIXME: if we changed the rootname from catwork in something other we got
    //        a problem here!!!
    render_apidoc('catwork/lib/catwork.pm','catwork.pm')
});

function get_apidoc_list () {

    jQuery.ajax({
        type: "POST",
        dataType: "html",
        async: false,
        url: "/apidoc/get/apidoc_list/",
        data: {},
        success: function post_process_result (data) {
            $("#runtime_js").empty();
            $("#runtime_js").append(data);
        }
      });

}

function search () {

    show_wait();
    remove_infos();
    remove_warnings();
    remove_errors();

    var search = $('#search').val();
    if(search) {
        jQuery.ajax({
            type: "POST",
            dataType: "json",
            async: false,
            url: "/ws/apidoc/get/search/json/",
            data: { search: search},
            success: function post_process_result (data) {
                $("#inner_content").empty();
                var search_result = "<h1>Search Results:</h1><ul>";
                var total_hits = 0;
                jQuery.each(data.result, function(path, value) {
                    search_result += "<li><a href=\"javascript:get_single_search_result('"+path+"','"+search+"');\">"+path+"</a> <b>("+value.hits+")</b></li>"
                    total_hits = total_hits + value.hits;
                });
                search_result += "</ul>Total found: "+total_hits;
                $("#summary_results").empty();
                $("#search_results").empty();
                $("#search_results").append(search_result);
                $("#footer").empty();
                $("#footer").append(data.footer);
                $("#header_subtitle").empty();
                $("#header_subtitle").append(data.header_subtitle);
                $.jGrowl("Search for <b>"+search+"</b> was proceded", { header: "Message:"});
            }
          });
    }
    else {
        var messages = new Array();
        messages.push("No search value given!");
        show_errors("Search Problem:",messages)
    }

    hide_wait();
}

function project_report() {

    jQuery.ajax({
        type: "POST",
        dataType: "json",
        async: false,
        url: "/ws/apidoc/get/project_report/json/",
        data: {},
        success: function post_process_result (data) {
            $("#summary_results").empty();
            $("#inner_content").empty();
            var project_report = "<h1>Project Report</h1><pre>"+data.result+"</pre>";
            $("#search_results").empty();
            $("#inner_content").empty();
            $("#inner_content").append(project_report);
            $("#footer").empty();
            $("#footer").append(data.footer);
            $("#header_subtitle").empty();
            $("#header_subtitle").append(data.header_subtitle);
            $.jGrowl("Project Report was proceded", { header: "Message:"});
        }
      });

}

function get_summary(PATH) {

    show_wait();

    jQuery.ajax({
        type: "POST",
        dataType: "json",
        async: false,
        url: "/ws/apidoc/get/summary/json/",
        data: { path: PATH },
        success: function post_process_result (data) {
            $("#search_results").empty();
            $("#inner_content").empty();
            $("#footer").empty();
            $("#footer").append(data.footer);
            $("#header_subtitle").empty();
            $("#header_subtitle").append(data.header_subtitle);

            $("#summary_results").empty();
            $("#summary_results").append("<h1>Summary</h1><h3>"+PATH+"</h3><table cellpadding='0' cellspacing='0' border='0' id='summary_table'></table>");
            $('#summary_table').dataTable( {
                "bInfo" : false,
                "bFilter" : true,
                "bPaginate" : true,
                "bJQueryUI": true,
                "sPaginationType" : 'full_numbers',
                "aaData": data.result,
                "aoColumns": [
                    { "sTitle": "Path"  },
                    { "sTitle": "Description" },
                    { "sTitle": "Revision" },
                    { "sTitle": "Author", "sWidth": "20%" },
                ]
            } );
        }
      });

    hide_wait();
}

